package test.test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.protocol.http.control.AuthManager;
import org.apache.jmeter.protocol.http.control.CacheManager;
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.DNSCacheManager;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.property.TestElementProperty;
import org.apache.jmeter.threads.JMeterVariables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




public class SubSample extends AbstractSampler{
	public static final Logger log=LoggerFactory.getLogger(SubSample.class);
	 public static final String text1 = "1"; //$NON-NLS-1$
	 public static final String text2 = "2"; //$NON-NLS-1$
	 private static final Set<String> APPLIABLE_CONFIG_CLASSES = new HashSet<>(
	            Arrays.asList(
	                  
	                    "test.SubSampleDefaultConfigGui"
	            ));
	private static final String Config_MANAGER = "subsample_config";
	//private String b="1";
	//private String a="1";
	public String getA() {
		return  getPropertyAsString(text1);
	}


	public void setA(String a) {
		this.setProperty(this.text1, a);
	}


	public String getB() {
		return getPropertyAsString(text2);
	}


	public void setB(String b) {
		this.setProperty(this.text2, b);;;
	}
	@Override
	public SampleResult sample(Entry arg0) {
		//trace("parctiseDemo01Sample");
		SampleResult res=new SampleResult();
		
		boolean isOK=false;//判断sample是否正常运行
		String response;
		res.setSampleLabel(this.getName());//这里有于设置产生的新的subSample名称
		
		res.sampleStart();
		try {
			
			res.setResultFileName("test1");
			
			int aa=Integer.valueOf(getA());
			int bb=Integer.valueOf(getB());
//			int aa=Integer.valueOf("1");
//			int bb=Integer.valueOf("2");
			int sum=aa+bb;
			response="当前线程是："+Thread.currentThread().getName()+"\r\n"+sum;
			res.setResponseData(response,"utf-8");
			res.setSamplerData("这里是samplerdata");
			res.setDataType(SampleResult.TEXT);
			res.setResponseCodeOK();
			res.setResponseMessage("程序运行正常");
			isOK=true;
			
		} catch (Exception e) {
			res.setResponseCode("500");
			res.setResponseMessage("ab都要输入int型数字啊");
			res.setResponseData("hehe",null);
		}
		//this.addTestElement(child);
		//log.info("1111111111111");
		res.sampleEnd();
		res.setSuccessful(isOK);
		String variables=getThreadContext().getVariables().get("test");
		log.info("参数test的值为："+variables);
		//String propertyAsString = getConfigTestElement().getPropertyAsString(SubSample.text1);
		
		//System.out.println("=============================="+propertyAsString);
		// getConfigTestElement().setProperty(this.text1,"555");
		// String propertyAsString2 = getConfigTestElement().getPropertyAsString(SubSample.text1);
		 
		// System.out.println("2=============================="+propertyAsString2);
		 
		
		return res;
	}
	@Override
    public boolean applies(ConfigTestElement configElement) {
        String guiClass = configElement.getProperty(TestElement.GUI_CLASS).getStringValue();
        return APPLIABLE_CONFIG_CLASSES.contains(guiClass);
    }
	
	
	 @Override
	    public void addTestElement(TestElement el) {
	        if (el instanceof ConfigTestElement) {
	        	super.addTestElement(el);//这句是把config的值赋到sample中
	            setConfigTestElement((ConfigTestElement) el);//这句是将config对像作为一个值保存到sample
	       
	        } else {
	            super.addTestElement(el);
	        }
	    }


	private void setConfigTestElement(ConfigTestElement value) {
		ConfigTestElement mgr = getConfigTestElement();
	        if (mgr != null) {
	            if(log.isWarnEnabled()) {
	                log.warn("Existing CookieManager {} superseded by {}", mgr.getName(), value.getName());
	            }
	        }
	        setConfigTestElementProperty(value);
		
	}


	private void setConfigTestElementProperty(ConfigTestElement value) {
		setProperty(new TestElementProperty(Config_MANAGER, value));
		System.out.println("===================");
	}


	private ConfigTestElement getConfigTestElement() {
		
		return (ConfigTestElement) getProperty(Config_MANAGER).getObjectValue();
	}
	
	
	
}

